package com.yabushan.web.service.impl;

import com.yabushan.common.exception.CustomException;
import com.yabushan.common.utils.StringUtils;
import com.yabushan.system.domain.YmxAsinTemplateInfo;
import com.yabushan.system.domain.vo.YmxAsinTemplateInfoVo;
import com.yabushan.system.service.IYmxAsinTemplateInfoService;
import com.yabushan.system.service.IYmxGiftInfoService;
import com.yabushan.web.mapper.LandingPageMapper;
import com.yabushan.web.model.entity.YmxCustomerInformationInfo;
import com.yabushan.web.model.entity.YmxGiftInfo;
import com.yabushan.web.model.entity.YmxOrderInfo;
import com.yabushan.web.model.vo.*;
import com.yabushan.web.service.LandingPageApiService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description BPMTimesExtApiServiceImpl
 * @Author huihui
 * @Date 2021-03-29 11:30:39
 * @Version 1.0
 */
@Service
public class LandingPageApiServiceImpl implements LandingPageApiService {

    @Resource
    private LandingPageMapper landingPageMapper;

    @Resource
    private IYmxAsinTemplateInfoService ymxAsinTemplateInfoService;

    @Resource
    private IYmxGiftInfoService ymxGiftInfoService;


    /**
     * 订单号匹配
     * @param orderInfoVO
     * @return
     */
    @Override
    public List<YmxOrderInfo> selectOrderMatching(YmxOrderInfoVO orderInfoVO) throws ParseException {
        List<YmxOrderInfo> ymxOrderInfoList = landingPageMapper.selectOrderMatching(orderInfoVO);
        int size = ymxOrderInfoList.size();
        if (size != 0) {
            for (int i = 0; i < size; i++) {
                YmxOrderInfo ymxOrderInfo = ymxOrderInfoList.get(i);
                String asin = ymxOrderInfo.getAsin();
                YmxAsinTemplateInfo ymxAsinTemplateInfo = new YmxAsinTemplateInfo();
                ymxAsinTemplateInfo.setAsin(asin);
                List<YmxAsinTemplateInfo> list = ymxAsinTemplateInfoService.selectYmxAsinTemplateInfoList(ymxAsinTemplateInfo);
                if (list.size() != 0) {
                    YmxAsinTemplateInfo asinTemplateInfoVo = list.get(0);
                    ymxOrderInfo.setDiscountCode(asinTemplateInfoVo.getDiscountCode());
                    ymxOrderInfo.setWarrantyStartDate(asinTemplateInfoVo.getWarrantyStartDate());
                    ymxOrderInfo.setWarrantyEndDate(asinTemplateInfoVo.getWarrantyEndDate());
                    ymxOrderInfo.setWarrantyDate(subMonth(asinTemplateInfoVo.getWarrantyStartDate(),asinTemplateInfoVo.getWarrantyEndDate()));
                    ymxOrderInfo.setOrderName(asinTemplateInfoVo.getOrderName());
                    ymxOrderInfo.setOrderImage(asinTemplateInfoVo.getOrderImage());
                } else {
                    ymxOrderInfo.setOrderName(ymxOrderInfo.getProductName());
                }
            }
        }
        return ymxOrderInfoList;
    }


    /****
     * 传入具体日期 ，返回具体日期增加一个月。
     * @param dt 日期(2017-04-13)
     * @return 2017-05-13
     * @throws ParseException
     */
    private  Date subMonth(Date dt,Integer month) throws ParseException {
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);*/
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, month);
        Date dt1 = rightNow.getTime();
        return dt1;
    }
    /**
     * 录入客户个人信息
     * @param infoVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRED)
    public int insertCustomerInformationInfo(YmxCustomerInformationInfoVo infoVo) {
        int result = 0;
        YmxCustomerInformationInfo informationInfo = new YmxCustomerInformationInfo();
        String uuid = StringUtils.getUUID();
        informationInfo.setCustomerInformationId(uuid);
        informationInfo.setCustomerName(infoVo.getCustomerName());
        informationInfo.setCustomerEmail(infoVo.getCustomerEmail());
        informationInfo.setCustomerFacebookId(infoVo.getCustomerFacebookId());
        result = landingPageMapper.insertCustomerInformationInfo(informationInfo);
        if (result == 0) {
            throw new CustomException("录入客户个人信息出错，请联系管理员！");
        }
        YmxOrderInfoVO orderInfoVO = new YmxOrderInfoVO();
        orderInfoVO.setCustomerInformationId(uuid);
        orderInfoVO.setOrderId(infoVo.getOrderId());
        result = landingPageMapper.updateOrderId(orderInfoVO);
        if (result == 0) {
            throw new CustomException("更新订单表客户信息出错，请联系管理员！");
        }
        return result;
    }

    /**
     * 礼物列表查询
     * @param giftInfoVO
     * @return
     */
    @Override
    public List<YmxGiftInfo> selectGiftInfo(YmxOrderGiftInfoVO giftInfoVO) {
        List<YmxGiftInfo> giftInfoList = new ArrayList<>();
        YmxAsinTemplateInfo ymxAsinTemplateInfo = new YmxAsinTemplateInfo();
        String asin = giftInfoVO.getAsin();
        ymxAsinTemplateInfo.setAsin(asin);
        List<YmxAsinTemplateInfo> list = ymxAsinTemplateInfoService.selectYmxAsinTemplateInfoList(ymxAsinTemplateInfo);
        int size = list.size();
        if (size != 0) {
            YmxAsinTemplateInfo asinTemplateInfoVo = list.get(0);
            String templateId = asinTemplateInfoVo.getTemplateId();
            List<com.yabushan.system.domain.YmxGiftInfo> ymxGiftInfoList = ymxGiftInfoService.selectGiftByTemplateId(templateId);
            int size1 = ymxGiftInfoList.size();
            if (size1 != 0) {
                for (int i = 0; i < size1; i++) {
                    YmxGiftInfo giftInfo = new YmxGiftInfo();
                    com.yabushan.system.domain.YmxGiftInfo ymxGiftInfo = ymxGiftInfoList.get(i);
                    giftInfo.setGiftId(ymxGiftInfo.getGiftId());
                    giftInfo.setGiftName(ymxGiftInfo.getGiftName());
                    giftInfo.setGiftImage(ymxGiftInfo.getGiftImage());
                    giftInfoList.add(giftInfo);
                }
            }
        }
        return giftInfoList;
    }

    /**
     * 记录客户所选礼物
     * @param giftInfoVO
     * @return
     */
    @Override
    public int recordCustomerGifts(YmxCustomerGiftInfoVO giftInfoVO) {
        int result = 0;
        String uuid = StringUtils.getUUID();
        giftInfoVO.setCustomerGiftId(uuid);
        result = landingPageMapper.recordCustomerGifts(giftInfoVO);
        if (result == 0) {
            throw new CustomException("记录客户所选礼物出错，请联系管理员！");
        }
        return result;
    }

    /**
     * 记录客户所选星级或评论
     * @param commentInfoVO
     * @return
     */
    @Override
    public int recordCustomerStarRating(YmxCommentInfoVO commentInfoVO) {
        int result = 0;
        String editCategory = commentInfoVO.getEditCategory();
        YmxCommentInfoVO ymxCommentInfoVO = landingPageMapper.selectOrderIdByComment(commentInfoVO.getOrderId());
       /* if(editCategory.equals("1") && landingPageMapper.selectOrderIdByComment(commentInfoVO.getOrderId())!=null){
            throw new CustomException("记录客户所选星级或评论出错，请联系管理员！");
        }*/
        if (!StringUtils.isEmpty(editCategory) ||editCategory.equals("1")) {
            String uuid = StringUtils.getUUID();
            commentInfoVO.setCommentId(uuid);
            result = landingPageMapper.recordCustomerStarRating(commentInfoVO);
        } else {
            YmxCommentInfoVO infoVO = landingPageMapper.selectOrderIdByComment(commentInfoVO.getOrderId());
            commentInfoVO.setCommentId(infoVO.getCommentId());
            result = landingPageMapper.updateCustomerStarRating(commentInfoVO);
        }
        if (result == 0) {
            throw new CustomException("记录客户所选星级或评论出错，请联系管理员！");
        }
        return result;
    }
}
